#include <stdlib.h>
#include <pthread.h>
#include <mTask.h>

struct testlist_t
{
    mListNode_t node;
    int len;
    char data[0];
};
void test_list(void)
{
    void *FreeArray[30]={0};
    int ArrayCount = 0;
    int i;
    mList_t myList;
    struct testlist_t *node;
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=0;i<50;i++)
    {
        node = (struct testlist_t*)malloc(sizeof(struct testlist_t)+2048);
        if(node!=NULL)
        {
            mListInsertFront(&myList,(mListNode_t*)node);
            if(ArrayCount<30)
                FreeArray[ArrayCount++] = node;
        }
    }
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=0;i<80;i++)
    {
        node = (struct testlist_t*)malloc(sizeof(struct testlist_t)+4096);
        if(node!=NULL)
        {
            mListInsertTail(&myList,(mListNode_t*)node);
        }
    }//insert 50+80 = 130
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=ArrayCount-1;i>=0;i--)
    {
        node=(struct testlist_t*)FreeArray[i];
        if(node!=NULL)
        {
            mListRemoveNode(&myList,(mListNode_t*)node);
        }
    }//remove 130-30 = 100
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=0;i<50;i++)
    {
        node = (struct testlist_t*)malloc(sizeof(struct testlist_t)+2048);
        if(node!=NULL)
        {
            mListInsertFront(&myList,(mListNode_t*)node);
        }
    }
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=0;i<80;i++)
    {
        node = (struct testlist_t*)malloc(sizeof(struct testlist_t)+4096);
        if(node!=NULL)
        {
            mListInsertTail(&myList,(mListNode_t*)node);
        }
    }//insert 100 + 50 + 80 = 230
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=0;i<20;i++)
    {
        node =(struct testlist_t*) mListDetachTail(&myList);
        if(node!=NULL)
        {
            free(node);
        }
    }//remove 230 - 20 = 210
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
    for(i=0;i<35;i++)
    {
        node =(struct testlist_t*) mListDetachFront(&myList);
        if(node!=NULL)
        {
            free(node);
        }
    }//remove 210 - 35 = 175
    mTaskPrint(1,"get list num:%d\r\n",mListCountNode(&myList));
}

int main(void)
{
    test_list();
    while(1)
    {
        usleep(100);
    }
}
